﻿-- =============================================
-- Author:		Stocksdale, jon
-- Create date: June 21
-- Description:	Finds ALL Relationships and creates a NOSQL UNION to fetch all relationships - internal and external
--                SLOOOOOOOWWWWWWW
-- =============================================
CREATE PROCEDURE UNIONALLRELATIONSHIPS
	-- Add the parameters for the stored procedure here
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	DECLARE @EXTVIEW nvarchar(max)
	DECLARE @COMBINEDVIEWS nvarchar(max)
	DECLARE @RELNAME nvarchar(100)
	DECLARE ExternalViewsCursor cursor for
	SELECT ShortRelationshipName FROM DefinedEntityRelationships
	DECLARE @NOSQL nvarchar(max)
	SET @NOSQL = ''
	Open ExternalViewsCursor
	Fetch NEXT FROM ExternalViewsCursor INTO  @RELNAME
	WHILE @@FETCH_STATUS = 0 BEGIN
		SET @NOSQL = @NOSQL + 'SELECT Entity1ID, Entity2ID, SRNameWithDir FROM {'+@RELNAME+'} UNION '	
		Fetch NEXT FROM ExternalViewsCursor INTO  @RELNAME	
	END
	close ExternalViewsCursor
	Deallocate ExternalViewsCursor
	SET @NOSQL = SUBSTRING(@NOSQL,0,LEN(@NOSQL)-5)

	EXEC ExecuteRafikiNOSQL @NOSQL=@NOSQL, @TD='CURRENT.Term'
END